package com.process.Imp.soce;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.process.dao.pojoDao.ScoreDao;
import com.process.domain.score.Score;
import com.process.domain.publi.Student;
import com.process.service.ExcelServ.StudentService;
import com.process.service.score.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: ScoreServiceImp
 * @Author: 殷凡卓
 * @Date: 2023/5/5 21:08
 */
@Service
public class ScoreServiceImp extends ServiceImpl<ScoreDao,Score> implements ScoreService {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ScoreDao scoreDao;

    @Override
    public void setMouses(long classId, long processid) {
        Score score = new Score();
//      本班所有学生By_classId
        List<Student> studentLists = studentService.list(new LambdaQueryWrapper<Student>()
                .eq(Student::getClassId, classId));

        Map<String, String> studentMaps = studentLists.stream()
                .collect(Collectors.toMap(Student::getStuPhone, Student::getStuName));
//      获取map的乱序keyset
        List<String> randomKeySet = getRandomKeySet(studentMaps);
        randomKeySet.addAll(getRandomKeySet(studentMaps));
        randomKeySet.addAll(getRandomKeySet(studentMaps));
        randomKeySet.addAll(getRandomKeySet(studentMaps));

        int peopleNum = 0;      //被分配者编号
        int check = 0;          //改几个人

//key是电话号码，studentMaps.get(key)是人名
        for (String key : randomKeySet) {
            Student student = studentLists.get(peopleNum);
            if (student.getStuPhone() == key) {
                System.out.println("不允许自己改自己，跳过");
                continue;
            }
            System.out.print(student.getStuName() + student.getStuPhone() + "分配到：");
            System.out.print(key + ":" + studentMaps.get(key) + "\n");
            score.setRaterPhone(student.getStuPhone());
            score.setAppraiseePhone(key);
            score.setProcessId(processid);
            score.setFraction(70);

            scoreDao.insert(score);

            check++;
            if (check % 3 == 0) {
                peopleNum++;
            }
            if (peopleNum == studentLists.size())
                break;
        }

    }

    /**
     * 乱序 map 集合的 keyset
     *
     * @param studentMaps
     * @return
     */
    public List<String> getRandomKeySet(Map<String, String> studentMaps) {
        Set<String> keysInSet = studentMaps.keySet();
        List<String> keysInList = new ArrayList<>(keysInSet.size());
        keysInList.addAll(keysInSet);
        Collections.shuffle(keysInList);
        return keysInList;
    }


    /**
     * 如果少于最少人数，append 追加
     *
     * @param studentMaps
     * @param appendMaps
     * @param checkNum
     * @return
     */
    public Map<String, String> appendMaps(Map<String, String> studentMaps, Map<String, String> appendMaps, int checkNum) {
        if (studentMaps.size() < checkNum) {
            studentMaps.putAll(appendMaps);
            return studentMaps;
        } else
            return studentMaps;
    }

    @Override
    public List<Score> getMyMouses(String stuPhone, long processId) {
        LambdaQueryWrapper<Score> wrapper = new LambdaQueryWrapper<Score>()
                .eq(Score::getProcessId, processId)
                .eq(Score::getRaterPhone, stuPhone);
        List<Score> scoreList = scoreDao.selectList(wrapper);
        return scoreList;
    }
}


